//
//  ST_TalkCupidViewController.swift
//  btvideo
//
//  Created by mac on 2021/4/8.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON



class ST_TalkCupidViewController: UIViewController {

    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        SCComponets.alert_dispatcher?.nextAlert()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if json["video_url"].stringValue.count > 2 {
            layout_video_style_view()
        }else{
            layout_normal_view()
        }
        
    }
    
    var json = JSON.null
    
    var type = TalkCupidViewType._PrivateConversation
    
    fileprivate func layout_normal_view(){
        
        view.addSubview(normal_view)
        
        normal_view.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(ST_TalkCupidNormalView.height)
        }
        
        normal_view.bind_data(json: json, type: type)
    }
    fileprivate func layout_video_style_view(){
        
        view.addSubview(video_style_view)
        
        let w = ScreenWidth - 20
        let h = w/355.0*545
        
        video_style_view.snp.makeConstraints { make in
            make.width.equalTo(w)
            make.height.equalTo(h)
            make.center.equalToSuperview()
        }
        
        video_style_view.bind_data(json: json, type: type)
    }
    
    
    
    //    MARK:- Properties
    fileprivate lazy var normal_view:ST_TalkCupidNormalView = {
        let control = ST_TalkCupidNormalView()
        control.chat_btn.addTarget(self, action: #selector(confirm_handle), for: .touchUpInside)
        control.close_btn.addTarget(self, action: #selector(close_handle), for: .touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var video_style_view:ST_TalkCupidVideoView = {
        let control = ST_TalkCupidVideoView()
        control.cornerRadius = 16
        control.clipsToBounds = true
        control.chat_btn.addTarget(self, action: #selector(confirm_handle), for: .touchUpInside)
        control.close_btn.addTarget(self, action: #selector(close_handle), for: .touchUpInside)
        return control
    }()
    
    fileprivate lazy var isVideo:Bool = {
        return json["type"].stringValue == "2"
    }()

}


//MARK: - Event
extension ST_TalkCupidViewController{
    
    @objc func confirm_handle(){
        
        if BTVerticalAlertViewController.toastBindPhoneRequireAlert(fromController: self) {
            return
        }
        
        if ST_MediaChatDispatcher.shared.live_alert_accid_set.contains(json["accid"].stringValue) == false{
            
            self.toast(text: "对方已取消")
            
            return
        }
        
        var eventID = "Chat_Receive"
        
        if isVideo{
            eventID = "Voice" + eventID
        }else{
            eventID = "Video" + eventID
        }
        
        var btnName = "接受通话_男"
        if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == .female{
            btnName = "接受通话_女"
        }
        
        MobClick.event(eventID, attributes: ["Button_name":btnName])
        
        
//        ST_FateAlertManager.shared.setCurrentAccountMediaTalkFateState(isFinish: true)
        
        goback_previous_controller(animated: true) {
            
            if self.type == .recommendSomeOne{
                
                ST_MediaChatDispatcher.shared.inviteChat(sessionId: self.json["accid"].stringValue, isVideo: self.isVideo,inviteDistract: "upLiveWall")
                
                SCComponets.alert_dispatcher?.finish_recommend_one_stack()
            }else{
                
                
                
                ST_MediaChatDispatcher.shared.apply_join_received_invite(json: self.json, isVideo: self.isVideo)
            }
        }
    }
    
    @objc fileprivate func close_handle(){
        
        self.goback_previous_controller(animated: false) {
            
            ST_MediaChatDispatcher.shared.push_cancel_another_invite(accid: self.json["from_accid"].stringValue, isVideo: self.isVideo, text: "对方已拒绝", stopVoice: false)
            
        }
        
        
    }
}
